﻿using ApplicationBase.Core.Context;
using IdentityService.Domain.Aggregates.PermitAggregate;
using IdentityService.Domain.Aggregates.RoleAggregate;
using IdentityService.Infrastructure;
using Microsoft.EntityFrameworkCore;

namespace IdentityService.Test
{
    public class TestDataGenerator
    {
        private readonly AppIdentityDbContext _dbContext;
        private readonly IServiceContext _serviceContext;

        public TestDataGenerator(AppIdentityDbContext dbContext, IServiceContext serviceContext)
            => (_dbContext, _serviceContext)
            = (dbContext, serviceContext);

        [Fact]
        public async Task InitTestData()
        {
            var superAdminPermit = await InitPermits();
            var superAdmin = await InitRoles(superAdminPermit);
        }

        private async Task<Permit> InitPermits()
        {
            await _dbContext.Permit.ExecuteDeleteAsync();

            var superAdmin = new Permit(_serviceContext.GenerateId(), "超级管理员");

            var identityAdmin = new Permit(_serviceContext.GenerateId(), "身份系统管理员");
            superAdmin.Children.Add(identityAdmin);
            var userAccountAdmin = new Permit(_serviceContext.GenerateId(), "账号管理员");
            var roleAdmin = new Permit(_serviceContext.GenerateId(), "角色管理员");
            var permitAdmin = new Permit(_serviceContext.GenerateId(), "通行证管理员");
            identityAdmin.Children.Add(userAccountAdmin);
            identityAdmin.Children.Add(roleAdmin);
            identityAdmin.Children.Add(permitAdmin);

            var noteAdmin = new Permit(_serviceContext.GenerateId(), "笔记系统管理员");
            superAdmin.Children.Add(noteAdmin);

            var noteUser = new Permit(_serviceContext.GenerateId(), "笔记系统用户");
            noteAdmin.Children.Add(noteUser);

            var visitor = new Permit(_serviceContext.GenerateId(), "游客");
            superAdmin.Children.Add(visitor);

            await _dbContext.Permit.AddAsync(superAdmin);
            await _dbContext.SaveChangesAsync();

            return superAdmin;
        }

        private async Task<Role> InitRoles(Permit superAdminPermit)
        {
            await _dbContext.RolePremit.ExecuteDeleteAsync();
            await _dbContext.Role.ExecuteDeleteAsync();

            var superAdmin = new Role(_serviceContext.GenerateId(), "超级管理员", App.Name);
            superAdmin.Permits.Add(new RolePermit(_serviceContext.GenerateId(), superAdmin.Id, superAdminPermit.Id));
            await _dbContext.Role.AddAsync(superAdmin);

            var identityAdmin = new Role(_serviceContext.GenerateId(), "身份系统管理员", App.Name);
            var identityAdminPermit = superAdminPermit.FindDescendantByName("身份系统管理员");
            identityAdmin.Permits.Add(new RolePermit(_serviceContext.GenerateId(), identityAdmin.Id, identityAdminPermit!.Id));
            await _dbContext.Role.AddAsync(identityAdmin);

            var accountRoleAdmin = new Role(_serviceContext.GenerateId(), "账号&角色管理员", App.Name);
            var accountAdminPermit = superAdminPermit.FindDescendantByName("账号管理员");
            var roleAdminPermit = superAdminPermit.FindDescendantByName("角色管理员");
            accountRoleAdmin.Permits.Add(new RolePermit(_serviceContext.GenerateId(), accountRoleAdmin.Id, accountAdminPermit!.Id));
            accountRoleAdmin.Permits.Add(new RolePermit(_serviceContext.GenerateId(), accountRoleAdmin.Id, roleAdminPermit!.Id));
            await _dbContext.Role.AddAsync(accountRoleAdmin);

            await _dbContext.SaveChangesAsync();

            return superAdmin;
        }
    }
}
